package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import static com.shanxia.codec.Decoder.*;

/**
 * S -> C
 * 服务器查询最近一次充电各时段信息
 * 服务器应答充电桩上报充电信息报文
 */
@Data
public class Cmd113 extends ProtocolDataBase {

    public static final int MIN_SIZE = 6;

    private int v1;
    private int v2;
    private int chargingPortNum;                // 充电口号
    private int sign;                           // 标志

    public Cmd113(){
        super.setCmd(113);
    }

    public static Cmd113 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd113 cmd = new Cmd113();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        //数据域长度
        int len = dataField.length;
        if (len < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        cmd.v1 = getUint16(dataField, offset);
        offset += 2;
        cmd.v2 = getUint16(dataField, offset);
        offset += 2;
        // 1字节充电口号
        cmd.chargingPortNum = getUint8(dataField[offset++]);
        // 1字节标志
        cmd.sign = getUint8(dataField[offset++]);
        return cmd;
    }

    @Override
    public byte[] getDataField() {
        byte[] data = new byte[MIN_SIZE];
        getBytesAsUint16(data, 0, this.v1);
        getBytesAsUint16(data, 2, this.v2);
        data[4] = (byte) this.chargingPortNum;
        data[5] = (byte) this.sign;
        return data;
    }
}
